import copy

import mmlib

import numpy as np

import matplotlib


def sa_test():
    unordered = np.random.random(50)

    print(unordered)

    def dist(arr0):
        arr=copy.deepcopy(arr0)
        pos1 = np.random.randint(0, len(arr))
        pos2 = np.random.randint(0, len(arr))

        while (pos1 == pos2):
            pos2 = np.random.randint(0, len(arr))

        temp = arr[pos1]
        arr[pos1] = arr[pos2]
        arr[pos2] = temp

        return arr

    def cost(arr):
        ret=0
        sorted=copy.deepcopy(arr)
        sorted.sort()
        for i,ie in enumerate(arr):
            ret+=np.abs(ie-sorted[i])

        return ret

    print(mmlib.simulated_annealing(
        unordered,
        dist,
        cost,
        iteration_count=100
    ))

sa_test()
